﻿import Node;
/**
 * An implementation of a linked Queue Datastructure. From Java.
 *
 *@author Michael Avila
 *@version 1.0.0
 */
class Queue {
    private var first:Node;     //資料連結的首端
    private var last:Node;      //資料連結的尾端
    
    public function isEmpty() {
        return (first == null);
    }
    
    public function enqueue(data:Object) {
        var node:Node = new Node();
        node.data = data;
        node.next = null;
        if (isEmpty()) {
            first = node;
            last = node;
        } else {
            last.next = node;
            last = node;
        }
    }
    
    public function dequeue() {
        if (isEmpty()) {
            trace("Error: \n\t Objects of type Queue must contain data before being dequeued.");
            return;
        }
        var data = first.data;
        first = first.next;
        return data;
    }
    
    public function peek() : Object
    {
        if (isEmpty()) {
            trace("Error: \n\t Objects of type Queue must contain data before you can peek.");
            return;
        }
        return first.data;
    }
}